package com.zy.explore.client.infrastructure.util.mock;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * description 关键字解析
 * 通过解析关键字，调用特定的 Faker 方法生成随机字符串
 * myUsername => faker.name().name()
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2022年04月17日 02:38:46
 */
public class KeywordResolver {

    static final List<Keyword> KEY_WORDS = Arrays.asList(
        new Keyword("username", "name.name", "en"),
        new Keyword("email", "internet.emailAddress", "en"),
        new Keyword("name", "name.name"),
        new Keyword("image", "internet.avatar"),
        new Keyword("pic", "internet.avatar"),
        new Keyword("avatar", "internet.avatar"),
        new Keyword("url", "internet.url"),
        new Keyword("phone", "phoneNumber.cellPhone"),
        new Keyword("address", "address.fullAddress"),
        new Keyword("title", "book.title")
    );

    public static String resolve(String key) {
        Optional<Keyword> canResolve = KEY_WORDS.stream()
            .filter(it -> key.contains(it.keyword))
            .findFirst();

        if (!canResolve.isPresent()) {
            throw new ResolveNotFoundException("[" + key + "] 没有匹配到关键字");
        }

        Keyword keyword = canResolve.get();
        return FakerNameResolver.resolve(keyword.key, keyword.lang);
    }

    static class Keyword {
        String keyword;
        String key;
        String lang;

        public Keyword(String keyword, String key) {
            this(keyword, key, "cn");
        }

        public Keyword(String keyword, String key, String lang) {
            this.keyword = keyword;
            this.key = key;
            this.lang = lang;
        }
    }
}
